
//The tree
var TopTag = new TagTree();
TopTag.treeReady = false;
TopTag.TagSet = new Array();

TopTag.buildTagSet = function(){
    this.TagSet.splice(0,this.TagSet.length);
    for(var i=0;i<RunArray.length;i++){
	this.TagSet.push(RunArray[i].tagList);
    }
    
    //for(var i=0;i<this.TagSet.length;i++)
    //alertToMonitor('init '+this.TagSet[i]);
    this.TagSet.sort();
    listToKill = new Array();
    for(var counter = 1;counter <  this.TagSet.length; counter++){
	kill = "";
	
	if( matchTagList(this.TagSet[counter], this.TagSet[counter-1])){
	    listToKill.push(counter);
	    kill = "kill";
	}
	//alertToMonitor(this.TagSet[counter] + " " + kill);
    }
    for(var i=listToKill.length-1;i>=0;i--){
	this.TagSet.splice(listToKill[i],1);
    }
    
    //for(var i=0;i<this.TagSet.length;i++)
    //alertToMonitor('fina '+this.TagSet[i]);
    
};
TopTag.buildTree = function(){
    thisTag = TopTag;
    thisTag.PropertyToMatch = this.TagSet[0];
    for(var i=1;i<this.TagSet.length;i++){ 
	new_tag = new TagTree();
	new_tag.PropertyToMatch = this.TagSet[i];
	thisTag.addSiblingTag(new_tag);
	thisTag = new_tag;
    }
    //dbg
    //thisTag = TopTag;
    //while( thisTag ){
    //alertToMonitor('chk '+thisTag.PropertyToMatch);
    //thisTag = thisTag.nextSibling;
    //}
};

TopTag.populateTree = function(){
    for(var i=0;i<RunArray.length;i++){
	thisTag = this;

	while( thisTag ){
	    if( thisTag.matchAndGrab(RunArray[i]) )
		break;  //break the while loop.
	    thisTag = thisTag.nextSibling;
	    }
    }
};
function RenderTagTree1(ListToRender){
    if( TopTag.treeReady == false ){
	TopTag.buildTagSet();  //Grabs tags from RunArray
	TopTag.buildTree();    //This might get more versions later.
	TopTag.populateTree();
	TopTag.treeReady = true;
    }

    thisTag = TopTag;

    var use_div = 1;
    if( use_div ){
	ParentTable = document.createElement('table');
	ParentBody = document.createElement('tbody');
	ParentTable.border = 2;
    }else{
	var ParentBody = null;
    }
    while(thisTag){
	//thisTag.RenderThisAndChildren(ListToRender,ParentBody);
	thisTag.RenderThisAndChildren_AllTable(ListToRender,ParentBody);
	thisTag = thisTag.nextSibling;
    }
    if( use_div ){
	ParentTable.appendChild(ParentBody);
	RenderBox = document.getElementById('RenderContent');
	RenderBox.appendChild(ParentTable);
    }
    
    return [300,300];
}

function TagTree(){
    this.PropertyToMatch = new Array();
    this.RunList = new Array();
    this.nextSibling = null;
    this.firstChild = null;
    this.lastChild = null;
    this.level = 0;
    this.test_text = '0';
}
TagTree.prototype.RenderThisAndChildren = function(ListToRender, ParentBody){
    var use_div = ( ParentBody ) ? false : true;
    //Label
    text = this.PropertyToMatch;
    if( text == '' ) text = 'No Tags';
    text_node = document.createTextNode(text);

    if(use_div){
	title = document.createElement('div');
	title.className = "tagTree_label_0";
	title.appendChild(text_node);
    }else{
	label_tr = document.createElement('tr');
	label_td = document.createElement('td');
	label_td.appendChild(text_node);
	label_tr.appendChild(label_td);
	ParentBody.appendChild(label_tr);
    }

    //Table.
    TableInBox = document.createElement('table');
    TableInBox.border = 2;
    Body = document.createElement('tbody');
    HeadRow=RenderHead(ListToRender);
    Body.appendChild(HeadRow);
    TableInBox.appendChild(Body);

    TableInBox.className="sortable";
    //TableInBox.style.display = "inline"; 
    sorttable.makeSortable(TableInBox);

    for(var i=0; i<this.RunList.length; i++){
	//alertToMonitor('run index '+i);
	RunRow=RenderRun(this.RunList[i],ListToRender);
	Body.appendChild(RunRow);
    }

    if( use_div ){
	container = document.createElement('div');
	container.className = "tagTree_container_0";
	container.appendChild(title);
	container.appendChild(TableInBox);
    
	RenderBox = document.getElementById('RenderContent');
	RenderBox.appendChild(container);
    }else{
	ParentBody.appendChild(TableInBox);
    }
}

TagTree.prototype.RenderThisAndChildren_AllTable = function(ListToRender, ParentBody){
    var use_div = ( ParentBody ) ? false : true;
    //Label
    text = this.PropertyToMatch;
    if( text == '' ) text = 'No Tags';
    text_node = document.createTextNode(text);

    label_tr = document.createElement('tr');
    label_td = document.createElement('td');
    label_td.setAttribute('colspan',ListToRender.length);
    label_td.appendChild(text_node);
    label_td.className = "tagTree_label_0";
    label_tr.appendChild(label_td);
    ParentBody.appendChild(label_tr);

    HeadRow=RenderHead(ListToRender);
    ParentBody.appendChild(HeadRow);


    for(var i=0; i<this.RunList.length; i++){
	//alertToMonitor('run index '+i);
	RunRow=RenderRun(this.RunList[i],ListToRender);
	//Body.appendChild(RunRow);
	ParentBody.appendChild(RunRow);
    }

}
TagTree.prototype.addChildTag = function(){

};
TagTree.prototype.addSiblingTag = function(newTag){
    this.nextSibling = newTag;
};
TagTree.prototype.matchAndGrab = function(match_this){
    returnvalue = false;
    if( matchTagList(this.PropertyToMatch, match_this.tagList) ){
	this.RunList.push(match_this);
	returnvalue = true;
    } 
    return returnvalue;
    
};

//This is so I only have to change it in once place.
function matchTagList(list1,list2){
    if( list1.toString() == list2.toString()){
	return true;
    }else{
	return false;
    }
}